package watermark;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import watermark.PictureCommonButton.ButtonStyle;

/**
 * 选择插入图片水印后的操作窗体
 * @author syt
 */
public class MainPictureFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private String toward[] = { "右下", "左下", "右上", "左上", "正中", "左上->右下",
            "左下->右下" };
    private String scales[] = {"不缩放", "25%", "50%", "75%","125%","150%","175%","200%"};
    private float imgScales[] = {1.0f, 0.25f, 0.5f, 0.75f, 1.25f, 1.5f, 1.75f, 2.0f};
    private JComboBox scaleCB, towardCB;
    private JTextField filepathTF, savepathTF, iconpathTF;
    private Border border = new BevelBorder(BevelBorder.RAISED);// 简单的双线斜面边框
    private Font font = new Font("微软雅黑", Font.PLAIN, 14);
    private int mainFrameWidth = 550;// MainPictureFrame的宽度
    private int mainFrameHeight = 410;// MainPictureFrame的高度
    private JButton selectPathBT, iconPathBT;
    private JProgressBar progressBar;

    private static final MainPictureFrame mainPictureFrame = new MainPictureFrame();
    public static MainPictureFrame getInstance() {
        return mainPictureFrame;
    }

    //关于组件外观的枚举
    protected enum UIClassName {
        systemLookAndFeel(UIManager.getSystemLookAndFeelClassName()),
        motifLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"),
        crossPlatformLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()),
        metalLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        private String className;

        UIClassName(String className) {
            this.className = className;
        }

        public String getUIClassName() {
            return className;
        }
    }

    private MainPictureFrame() {
        setUI(UIClassName.systemLookAndFeel);// 设置外观
        setTitle("添加水印");
        setSize(mainFrameWidth, mainFrameHeight);
        Common.setCentered(this);// 居中显示
        setLayout(new BorderLayout()); //设置布局为边框布局，边框布局分东南西北中5个方位来添加控件。

        JPanel panel = getNorthPanel();
        add(panel, BorderLayout.NORTH);

        panel = getCenterPanel();
        add(panel, BorderLayout.CENTER);

        panel = getSouthPanel();
        add(panel, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private JPanel getNorthPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBorder(new TitledBorder(border, "文件存取",
                Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_LEFT_TO_RIGHT, font));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.gridx = 0;
        gbc.weightx = 1;

        String filepath = WaterMark.getInstance().getSrcImgPath();
        filepathTF = new JTextField(filepath);
        panel.add(filepathTF, gbc);

        iconpathTF = new JTextField();
        panel.add(iconpathTF, gbc);

        String savePath = filepath.substring(0,filepath.lastIndexOf("\\")) + Common.getNewFileorDirName(filepath);
        savepathTF = new JTextField(savePath);
        panel.add(savepathTF, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0;

        selectPathBT = makeButton(ButtonStyle.selectImage, panel, gbc);
        iconPathBT = makeButton(ButtonStyle.selectIcon, panel, gbc);
        makeButton(ButtonStyle.selectSavepath, panel, gbc);

        Common.setComponentsFont(panel, font);
        return panel;

    }

    private JPanel getCenterPanel() {

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBorder(new TitledBorder(border, "设置",
                Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_LEFT_TO_RIGHT, font));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.fill = GridBagConstraints.BOTH;

        JLabel label;
        gbc.gridwidth = 1;
        gbc.weightx = 1.0;
        label = new JLabel("水印位置:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        towardCB = new JComboBox(toward);
        panel.add(towardCB, gbc);

        gbc.gridwidth = 1;
        label = new JLabel("缩放大小:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        scaleCB = new JComboBox(scales);
        panel.add(scaleCB, gbc);

        gbc.gridwidth = 1;
        label = new JLabel("执行进度:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        progressBar = new JProgressBar(0, 1);
        progressBar.setStringPainted(true);
        panel.add(progressBar, gbc);
        Common.setComponentsFont(panel, font);
        return panel;
    }

    private JPanel getSouthPanel() {
        JPanel panel = new JPanel();
        makeButton(ButtonStyle.preview, panel);
        makeButton(ButtonStyle.drirect, panel);
        Common.setComponentsFont(panel, font);
        return panel;
    }

    /**
     * 制按钮 两个参数
     * @param style 枚举ButtonStyle中的一个值
     * @param panel 放置该按钮的JPanel
     * @return JButton
     */
    private JButton makeButton(ButtonStyle style, JPanel panel) {
        return makeButton(style, panel, null);
    }
    /**
     * 制按钮 三个参数
     * @param style 枚举ButtonStyle中的一个值
     * @param panel 放置该按钮的JPanel
     * @param constraints 布局
     * @return JButton
     */
    private JButton makeButton(ButtonStyle style, JPanel panel, Object constraints) {
        JButton button = new PictureCommonButton(style);
        if (constraints == null) {
            panel.add(button);
        } else {
            panel.add(button, constraints);
        }
        return button;
    }
    /**
     * 设置外观
     * @param className 枚举UIClassName
     */
    protected void setUI(UIClassName className) {
        try {
            UIManager.setLookAndFeel(className.getUIClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取所要添加水印的文件名
     * @return
     */
    public String getFilepath() {
        return filepathTF.getText();
    }
    /**
     * 获取水印图片的路径
     * @return
     */
    public String  getIconpath() {
        return iconpathTF.getText();
    }
    /**
     * 获取添加水印后保存文件的路径
     * @return
     */
    public String getSavePath() {
        return savepathTF.getText();
    }
    /**
     * @return 放置将要添加水印的文件路径的JTextField
     */
    public JTextField getFilepathTF() {
        return filepathTF;
    }
    /**
     *
     * @return 放置添加的水印的文件路径的JTextField
     */
    public JTextField getIconpathTF() {
        return iconpathTF;
    }
    /**
     * @return 放置将要添加水印后保存路径的JTextField
     */
    public JTextField getSavepathTF() {
        return savepathTF;
    }
    /**
     * @return 水印的位置
     */
    public int getToward() {
        return towardCB.getSelectedIndex();
    }
    /**
     *
     * @return 放缩比例
     */
    public float getScale() {
        return imgScales[scaleCB.getSelectedIndex()];
    }
    /**
     *
     * @return 进度条
     */
    public JProgressBar getProgressBar() {
        return progressBar;
    }
}

